package net.xiaosaguo.mymall.product.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * description: 线程池配置
 *
 * @author xiaosaguo
 * @date 2020/07/04
 */

/*
 * 如果在 ThreadPoolConfigProperties 上不写 @Component，就用下面的代码，如果用了下面的代码，就不用加 @Component
 * @EnableConfigurationProperties(ThreadPoolConfigProperties.class)
 */
@Configuration
public class ThreadPoolConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {
        return new org.apache.tomcat.util.threads.ThreadPoolExecutor(
                pool.getCoreSize(),
                pool.getMaxSize(),
                pool.getKeepAliveTime(), TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }

}
